package com.paranoidvulture.javaEngine.MainPackage;
import java.awt.DisplayMode;
import java.awt.Graphics2D;
import java.awt.GraphicsConfiguration;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.Window;
import java.awt.image.BufferStrategy;
import java.awt.image.BufferedImage;

import javax.swing.JFrame;

public class ScreenManager 
{
	private GraphicsDevice vc;
	
	//give vc access to moinitor's screen
	public ScreenManager()
	{
		GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment();
		vc = env.getDefaultScreenDevice();
	}
	
	//get all compatible DMs
	public DisplayMode[] getCompatibleDisplayModes()
	{
		return vc.getDisplayModes();
	}
	
	//compare DM passed in to the VC's DM and see if they match
	public DisplayMode findFirstCompatibleMode(DisplayMode	modes[])
	{
		DisplayMode goodModes[] = vc.getDisplayModes();
		for(int x = 0; x < modes.length; x++)
		{
			for(int y = 0; y < goodModes.length; y++)
			{
				if (displayModesMatch(modes[x],goodModes[y]))
					return modes[x];
			}
		}
		return null;
	}
	
	//get current DM
	public DisplayMode getCurrentDisplayMode()
	{
		return vc.getDisplayMode();
	}
	
	//compare display modes
	public boolean displayModesMatch(DisplayMode x, DisplayMode y)
	{
		if (x.getWidth() != y.getWidth() || x.getHeight() != y.getHeight())
			return false;
		if(x.getBitDepth() != DisplayMode.BIT_DEPTH_MULTI && y.getBitDepth() != DisplayMode.BIT_DEPTH_MULTI
				&& x.getBitDepth() != y.getBitDepth())
			return false;
		if(x.getRefreshRate() != DisplayMode.REFRESH_RATE_UNKNOWN && y.getRefreshRate() != DisplayMode.REFRESH_RATE_UNKNOWN
				&& x.getRefreshRate() != y.getRefreshRate())
			return false;
		return true;
	}
	
	//make frame full screen
	public void setFullScreen(DisplayMode dm)
	{
		JFrame f = new JFrame();
		f.setUndecorated(true);
		f.setIgnoreRepaint(true);
		f.setResizable(false);
		vc.setFullScreenWindow(f);
		
		if(dm != null && vc.isDisplayChangeSupported())
		{
			try
			{
				vc.setDisplayMode(dm);
			}catch(Exception ex){}
		}
		f.createBufferStrategy(2);
		
	}
	
	//we will set Graphices object = to this
	public Graphics2D getGraphics()
	{
		Window w = vc.getFullScreenWindow();
		if(w != null)
		{
			BufferStrategy s = w.getBufferStrategy();
			return (Graphics2D)s.getDrawGraphics();
		}else
		{
			return null;
		}
	}
	
	//update
	public void update()
	{
		Window w = vc.getFullScreenWindow();
		if (w != null)
		{
			BufferStrategy s = w.getBufferStrategy();
			if (!s.contentsLost())
			{
				s.show();
			}
		}
	}
	
	//return full screen window
	public Window getFullScreenWindow()
	{
		return vc.getFullScreenWindow();
	}
	
	//get window width
	public int getWidth()
	{
		Window w = vc.getFullScreenWindow();
		if (w != null)
		{
			return w.getWidth();
		}else return 0;
	}
	
	//get window height
	public int getHeight()
	{
		Window w = vc.getFullScreenWindow();
		if (w != null)
		{
			return w.getHeight();
		}else return 0;
	}
	
	//get out of full screen
	public void restoreScreen()
	{
		Window w = vc.getFullScreenWindow();
		if (w != null)
		{
			w.dispose();
		}
		vc.setFullScreenWindow(null);
	}
		
	//create image compatible with monitor
	public BufferedImage createCompatibleImage(int w, int h, int t)
	{
		Window win = vc.getFullScreenWindow();
		if (win != null)
		{
			GraphicsConfiguration gc = win.getGraphicsConfiguration();
			return gc.createCompatibleImage(w, h,t);
		}else return null;
	}
}

















